# -*- coding: utf-8 -*-
import re
import datetime
from google.appengine.ext import webapp

register = webapp.template.create_template_register()

def escapebreak(body):
	return re.sub('\n', r'\\', body)

def render_app(app):
	app_list = {'sdb':'leedo\'s test app'}
	if app in app_list:
		return app_list[app]
	return app

def render(body):
	lien = re.compile(
		r'\[lien=(https?://[\w\d:#@%/;$()~_?\+-=\\\.&]*)\](.*?)\[/lien\]',
		re.MULTILINE|re.DOTALL)
	auto = re.compile(
		r'(https?://[\w\d:#@%/;$()~_?\+-=\\\.&]*)[^\[]',
		re.MULTILINE|re.DOTALL)

	body = re.sub(re.compile('\[g\](.*?)\[/g\]', re.MULTILINE|re.DOTALL),
							r'<strong>\1</strong>', body)
	body = re.sub(re.compile('\[i\](.*?)\[/i\]', re.MULTILINE|re.DOTALL),
							r'<i>\1</i>', body)
	body = re.sub(re.compile('\[cite\](.*?)\[/cite\]', re.MULTILINE|re.DOTALL),
							r'<cite>\1</cite>', body)
	body = re.sub(re.compile('\[bad\](.*?)\[/bad\]', re.MULTILINE|re.DOTALL),
							r'<big>\1</big>', body)
	body = re.sub(lien, r'<a href="\1">\2</a>', body)
	body = re.sub(r'\[/?(g|i||cite|bad)]', '', body)
	body = render_smiley(body)
	return body

def render_smiley(body):
	# smiley
	imgnode = '<img src="/img/smiley/%s.gif" alt="%s" />'
	body = re.sub(r':\)', imgnode % ('h_smile', ':)'), body)
	body = re.sub(r':\(', imgnode % ('h_sad', ':('), body)
	body = re.sub(r';\)', imgnode % ('h_blink', ';)'), body)
	body = re.sub(r':O', imgnode % ('h_surprise', ':O'), body)
	body = re.sub(r':D', imgnode % ('h_lol', ':D'), body)
	body = re.sub(r':hmm:', imgnode % ('h_exas', ':hmm:'), body)
	body = re.sub(r':\|:', imgnode % ('h_neutral', ':|'), body)
	body = re.sub(r':grr:', r'<img src="/img/smiley/h_rage.gif" alt=":grr:" />', body)
	body = re.sub(r':zzz:', r'<img src="/img/smiley/h_sleep.gif" alt=":zzz:" />', body)
	body = re.sub(r':wink:', r'<img src="/img/smiley/h_wink.gif" alt=":wink:" />', body)
	body = re.sub(r':horreur:', r'<img src="/img/smiley/h_horror.gif" alt=":horreur:" />', body)
	body = re.sub(r':zhead:', r'<img src="/img/smiley/h_zhead.gif" alt=":zhead:" />', body)
	body = re.sub(r':erk:', r'<img src="/img/smiley/h_sick.gif" alt=":erk:" />', body)
	# icon
	body = re.sub(r':home:', r'<img src="/img/smiley/h_home.gif" alt=":home:" />', body)
	body = re.sub(r':porte:', r'<img src="/img/smiley/h_door.gif" alt=":porte:" />', body)
	body = re.sub(r':puits:', r'<img src="/img/smiley/h_well.gif" alt=":puits:" />', body)
	body = re.sub(r':eau:', r'<img src="/img/smiley/h_water.gif" alt=":eau:" />', body)
	body = re.sub(r':zombie:', r'<img src="/img/smiley/h_zombie.gif" alt=":zombie:" />', body)
	body = re.sub(r':humain:', r'<img src="/img/smiley/h_human.gif" alt=":human:" />', body)
	body = re.sub(r':soin:', r'<img src="/img/smiley/h_heal.gif" alt=":soin:" />', body)
	body = re.sub(r':drogue:', r'<img src="/img/smiley/h_drug.gif" alt=":drogue:" />', body)
	body = re.sub(r':dig:', r'<img src="/img/smiley/h_dig.gif" alt=":dig:" />', body)
	body = re.sub(r':mort:', r'<img src="/img/smiley/h_death.gif" alt=":mort:" />', body)
	body = re.sub(r':gard:', r'<img src="/img/smiley/h_guard.gif" alt=":gard:" />', body)
	body = re.sub(r':eclair:', r'<img src="/img/smiley/h_ranger.gif" alt=":eclair:" />', body)
	body = re.sub(r':collec:', r'<img src="/img/smiley/h_collec.gif" alt=":collec:" />', body)
	body = re.sub(r':hab:', r'<img src="/img/smiley/h_basic.gif" alt=":hab:" />', body)
	body = re.sub(r':os:', r'<img src="/img/smiley/h_bone.gif" alt=":os:" />', body)
	body = re.sub(r':fleche:', r'<img src="/img/smiley/h_arrow.gif" alt=":fleche:" />', body)
	body = re.sub(r':sac:', r'<img src="/img/smiley/h_bag.gif" alt=":sac:" />', body)
	body = re.sub(r':ame:', r'<img src="/img/smiley/h_ghost.gif" alt=":ame:" />', body)
	body = re.sub(r':atelier:', r'<img src="/img/smiley/h_refine.gif" alt=":atelier:" />', body)
	body = re.sub(r':!!:', r'<img src="/img/smiley/h_warning.gif" alt=":!!:" />', body)
	body = re.sub(r':pa:', r'<img src="/img/smiley/h_pa.gif" alt=":pa:" />', body)
	body = re.sub(r':\*:', r'<img src="/img/smiley/h_middot.gif" alt=":*:" />', body)
	body = re.sub(r':ban:', r'<img src="/img/smiley/h_ban.gif" alt=":ban:" />', body)
	body = re.sub(r':chat:', r'<img src="/img/smiley/h_chat.gif" alt=":chat:" />', body)
	body = re.sub(r':calim:', r'<img src="/img/smiley/h_calim.gif" alt=":calim:" />', body)
	body = re.sub(r':annu:', r'<img src="/img/smiley/h_sites.gif" alt=":annu:" />', body)
	body = re.sub(r':evo:', r'<img src="/img/smiley/h_city_up.gif" alt=":evo:" />', body)
	body = re.sub(r':arma:', r'<img src="/img/smiley/h_arma.gif" alt=":arma:" />', body)
	body = re.sub(r':deco:', r'<img src="/img/smiley/h_deco.gif" alt=":deco:" />', body)
	body = re.sub(r':camp:', r'<img src="/img/smiley/h_camp.gif" alt=":camp:" />', body)
	body = re.sub(r':ermit:', r'<img src="/img/smiley/h_hunter.gif" alt=":ermit:" />', body)
	body = re.sub(r':bichon:', r'<img src="/img/smiley/h_tamer.gif" alt=":bichon:" />', body)
	return body

def render_date(body):
	day  = datetime.timedelta(days=1)
	now  = datetime.datetime.today()

	if body + day > now:
	# if the message is newer than a day
		diff = now - body
		diff = diff.seconds/60
		if diff < 60:
			if diff < 2:
				return 'à l\'instant.'
			return 'il y a %s minutes.' % diff
		diff = diff/60
		if diff == 1:
			return 'il y a %s heure.' % diff 
		return 'il y a %s heures.' % diff 
	# else, print absolute date
	month = {
		1	: 'Janvier',	2	: 'Fevrier',
		3	: 'Mars',		4	: 'Avril',
		5	: 'Mai',		6	: 'Juin',
		7	: 'Juillet',	8	: 'Aout',
		9	: 'Septembre',	10	: 'Octobre',
		11	: 'Novembre',	12	: 'Decembre',
		}[body.month]
	return 'le %s %s, à %s:%s' % (body.day, month,
							str(body.hour).zfill(2), str(body.minute).zfill(2))

register.filter(escapebreak)
register.filter(render)
register.filter(render_date)
register.filter(render_app)
